package com.example.myapplication.module_common_service;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.data.BookBorrow;

import java.util.ArrayList;
import java.util.List;

public class BooksFragment extends Fragment {
    List<BookBorrow> dataSource = new ArrayList<>();

    ListView listBooks;
    public BooksFragment() {
        // Required empty public constructor
    }

    public static BooksFragment newInstance(String param1, String param2) {
        BooksFragment fragment = new BooksFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }
    private void initData(){
        this.dataSource.add(
                new BookBorrow(1,"https://img1.baidu.com/it/u=638710027,2637731786&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=697","编译原理","2022-10-1","30")
                );
        this.dataSource.add(
                new BookBorrow(1,"https://img2.baidu.com/it/u=1329751944,1566495324&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=683","哲学导论","2022-10-1","30")
                );
        this.dataSource.add(
                new BookBorrow(1,"https://img1.baidu.com/it/u=1386350457,3635121246&fm=253&fmt=auto&app=138&f=JPEG?w=248&h=346","艺术入门","2022-10-1","30")
        );
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View  view = inflater.inflate(R.layout.fragment_books, container, false);
        listBooks = view.findViewById(R.id.list_books);
        initData();
        listBooks.setDividerHeight(10);
        listBooks.setAdapter(new BookAdapter(dataSource,getLayoutInflater(),getContext()));
        return view;
    }
    private class BookAdapter extends BaseAdapter {
        private List<BookBorrow> data;
        private LayoutInflater layoutInflater;
        private Context context;
        /**
         * 组件集合，对应list.xml中的控件
         *
         * @author Administrator
         */
        public final class ViewHolder {
            public ImageView cover_book;
            public TextView book_name;
            public TextView borrow_time;
            public TextView limit;
        }

        public BookAdapter(List<BookBorrow> data, LayoutInflater layoutInflater, Context context) {
            this.data = data;
            this.layoutInflater = layoutInflater;
            this.context = context;
        }

        @Override
        public int getCount() {
            return data.size() ;
        }

        @Override
        public Object getItem(int i) {
            return data.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {
            BookAdapter.ViewHolder viewHolder = null;

            if (convertView == null) {
                viewHolder = new BookAdapter.ViewHolder();
                //获得组件，实例化组件
                convertView = layoutInflater.inflate(R.layout.layout_for_book_borrow_display, null);
                viewHolder.cover_book = convertView.findViewById(R.id.book_cover);
                viewHolder.book_name = convertView.findViewById(R.id.bookName);
                viewHolder.borrow_time = convertView.findViewById(R.id.borrow_time);
                viewHolder.limit = convertView.findViewById(R.id.limit);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (BookAdapter.ViewHolder) convertView.getTag();
            }
            //绑定数据
            Glide.with(context).load(this.data.get(position).getCover()).into(viewHolder.cover_book);
            viewHolder.book_name.setText(this.data.get(position).getBookName());
            viewHolder.borrow_time.setText("借日："+this.data.get(position).getBorrowTime());
            viewHolder.limit.setText("时长："+this.data.get(position).getLimit());
            return convertView;
        }
    }


}